package com.dengc.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dengc.springboot.common.Result;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.dengc.springboot.service.IRoleService;
import com.dengc.springboot.entity.Role;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-04-26
 */
@RestController
@RequestMapping("/role")
public class RoleController {

        @Resource
        private IRoleService roleService;

        @PostMapping
        public Result save(@RequestBody Role role) {
            return Result.success(roleService.saveOrUpdate(role));
        }

        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
            return Result.success(roleService.removeById(id));
        }

        @GetMapping
        public Result findAll() {
            return Result.success(roleService.list());
        }

        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
            return Result.success(roleService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids){
                return Result.success(roleService.removeByIds(ids));
        }

        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String name) {
                LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(!"".equals(name),Role::getName,name);
                queryWrapper.orderByDesc(Role::getId);
                return Result.success(roleService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }

        /*
        *
        * 绑定角色和菜单的关系
        *  roleID 角色id
        *  menuIds 菜单id数组
        * */
        @PostMapping("/roleMenu/{roleId}")
        public Result roleMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
                roleService.setRoleMenu(roleId,menuIds);
                return Result.success();
        }
        @GetMapping("/roleMenu/{roleId}")
        public Result getRoleMenu(@PathVariable Integer roleId) {
                return Result.success( roleService.getRoleMenu(roleId));
        }
}
